package com.example.testproject;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.atomic.AtomicBoolean;

public class BookManagerService extends Service {
    public static final  String TAG = "BMS";
    private AtomicBoolean mIsServiceDestoryed = new AtomicBoolean(false);
    private CopyOnWriteArrayList<IOneBookArrivedListener> mListenerList = new CopyOnWriteArrayList<IOneBookArrivedListener>();
    private CopyOnWriteArrayList<Book> mBookList = new CopyOnWriteArrayList<Book>();
    private Binder mBinder = new IBookManager.Stub() {
        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public List<Book> getBookList() throws RemoteException {
            return mBookList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            mBookList.add(book);
        }

        @Override
        public void registerListener(IOneBookArrivedListener listener) throws RemoteException {
            if(!mListenerList.contains(listener)){
               mListenerList.add(listener);
            }else{
                Log.d(TAG,"已存在");
            }
            Log.d(TAG,"找到数量"+mListenerList.size());
        }

        @Override
        public void unregisterListener(IOneBookArrivedListener listener) throws RemoteException {
            if(mListenerList.contains(listener)){
                mListenerList.remove(listener);
                Log.d(TAG,"成功解绑");
            }else{
                Log.d(TAG,"未找到");
            }
            Log.d(TAG,"未找到数量"+mListenerList.size());
        }


    };

    @Override
    public void onCreate() {
        super.onCreate();
        mBookList.add(new Book(1,"android"));
        mBookList.add(new Book(2,"ios"));
        new Thread(new ServiceWorker()).start();

    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    @Override
    public void onDestroy() {
        mIsServiceDestoryed.set(true);
        super.onDestroy();
    }
    private void  onNewBookArrived(Book book) throws RemoteException{
        for (int i = 0; i < mListenerList.size(); i++) {
            IOneBookArrivedListener listener = mListenerList.get(i);
            listener.onNewBookArrived(book);
        }
    }
    private class ServiceWorker implements  Runnable{

        @Override
        public void run() {
            while (!mIsServiceDestoryed.get()){
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                int bookid = mBookList.size()+1;
                Book newBook = new Book(bookid,"new book ask"+bookid);
                try {
                    onNewBookArrived(newBook);
                } catch (RemoteException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
